//
//  HomeTitleView.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/9/29.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import SwiftFontName

class HomeTitleView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        let leftLabel = UILabel()
        addSubview(leftLabel)
        leftLabel.translatesAutoresizingMaskIntoConstraints = false
        leftLabel.text = "67kg"
        leftLabel.font = UIFont(name: FontName.PingFangSCRegular, size: 18)
        leftLabel.textColor = .white
        NSLayoutConstraint.activate([
            leftLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
            NSLayoutConstraint(item: leftLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 0.2, constant: 0),
            ])

        let leftDetailLabel = UILabel()
        addSubview(leftDetailLabel)
        leftDetailLabel.translatesAutoresizingMaskIntoConstraints = false
        leftDetailLabel.text = "体重"
        leftDetailLabel.font = UIFont(name: FontName.PingFangSCRegular, size: 12)
        leftDetailLabel.textColor = .white
        NSLayoutConstraint.activate([
            leftDetailLabel.topAnchor.constraint(equalTo: leftLabel.bottomAnchor, constant: 3),
            leftDetailLabel.centerXAnchor.constraint(equalTo: leftLabel.centerXAnchor),
            ])

        let rightLabel = UILabel()
        addSubview(rightLabel)
        rightLabel.translatesAutoresizingMaskIntoConstraints = false
        rightLabel.text = "67kg"
        rightLabel.font = UIFont(name: FontName.PingFangSCRegular, size: 18)
        rightLabel.textColor = .white
        NSLayoutConstraint.activate([
            rightLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
            NSLayoutConstraint(item: rightLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.8, constant: 0),
            ])

        let rightDetailLabel = UILabel()
        addSubview(rightDetailLabel)
        rightDetailLabel.translatesAutoresizingMaskIntoConstraints = false
        rightDetailLabel.text = "步数"
        rightDetailLabel.font = UIFont(name: FontName.PingFangSCRegular, size: 12)
        rightDetailLabel.textColor = .white
        NSLayoutConstraint.activate([
            rightDetailLabel.topAnchor.constraint(equalTo: rightLabel.bottomAnchor, constant: 3),
            rightDetailLabel.centerXAnchor.constraint(equalTo: rightLabel.centerXAnchor),
            ])

        let centerLabel = UILabel()
        addSubview(centerLabel)
        centerLabel.translatesAutoresizingMaskIntoConstraints = false
        centerLabel.text = "67"
        centerLabel.font = UIFont(name: FontName.PingFangSCRegular, size: 48)
        centerLabel.textColor = .white
        NSLayoutConstraint.activate([
            centerLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
            centerLabel.centerXAnchor.constraint(equalTo: centerXAnchor)
            ])

        let centerDetailLabel = UILabel()
        addSubview(centerDetailLabel)
        centerDetailLabel.translatesAutoresizingMaskIntoConstraints = false
        centerDetailLabel.text = "67kg"
        centerDetailLabel.font = UIFont(name: FontName.PingFangSCRegular, size: 12)
        centerDetailLabel.textColor = .white
        NSLayoutConstraint.activate([
            centerDetailLabel.topAnchor.constraint(equalTo: centerLabel.bottomAnchor, constant: 0),
            centerDetailLabel.centerXAnchor.constraint(equalTo: centerXAnchor)
            ])
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        addGradientLayer()
        addMaskLayer()
        addCircleLayer()
        addProgressLayer()
    }

    private func addProgressLayer() {
        let circleLayer = CAShapeLayer()
        let circlePath = UIBezierPath(roundedRect:
            CGRect(origin: CGPoint(x: center.x - 68, y: center.y - 68), size: CGSize(width: 136, height: 136)),
                                      cornerRadius: 68)
        circleLayer.path = circlePath.cgPath
        circleLayer.strokeColor = UIColor.white.cgColor
        circleLayer.lineWidth = 8
        circleLayer.fillColor = UIColor.clear.cgColor

        let gradient = CAGradientLayer()
        gradient.frame = frame
        gradient.colors = [UIColor("#5FFBFE").cgColor,
                           UIColor("#F2FF4F").cgColor]
        gradient.startPoint = CGPoint(x: 0, y: 1)
        gradient.endPoint = CGPoint(x: 1, y: 0)
        gradient.mask = circleLayer

        layer.addSublayer(gradient)
    }

    private func addCircleLayer() {
        let circleLayer = CAShapeLayer()
        let circlePath = UIBezierPath(roundedRect:
            CGRect(origin: CGPoint(x: center.x - 58, y: center.y - 58), size: CGSize(width: 116, height: 116)),
                                      cornerRadius: 58)
        circleLayer.path = circlePath.cgPath
        circleLayer.strokeColor = UIColor.white.cgColor
        circleLayer.fillColor = UIColor.clear.cgColor
        layer.addSublayer(circleLayer)
    }

    private func addGradientLayer() {
        let colorTop = UIColor("#00C8C2").cgColor
        let colorBottom = UIColor("#007EC6").cgColor

        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = [colorTop, colorBottom]
        gradientLayer.locations = [0.0, 1.0]
        layer.insertSublayer(gradientLayer, at: 0)
    }

    private func addMaskLayer() {
        let maskLayer = CAShapeLayer()
        let circlePath = UIBezierPath(roundedRect: bounds,
                                      byRoundingCorners: [.bottomLeft, .bottomRight],
                                      cornerRadii: CGSize(width: 20, height: 20))
        maskLayer.path = circlePath.cgPath
        layer.addSublayer(maskLayer)
        layer.mask = maskLayer
    }
}
